package com.yirui.bridge.aspect;

import com.yirui.bridge.annotation.SourceType;
import com.yirui.bridge.core.SourceTypeContext;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * 设置源类型
 * @author Dujie
 * @create 2025/7/25
 * @since 1.0.0
 */
@Aspect
@Component
public class SourceTypeAspect {

    @Pointcut("within(@com.yirui.bridge.annotation.SourceType *)")
    public void switchDataType() {

    }

    // 2) 环绕通知
    @Around("switchDataType()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        // 拿到类上的注解
        SourceType annotation = pjp.getTarget()
                .getClass()
                .getAnnotation(SourceType.class);

        SourceTypeContext.setType(annotation.value());
        try {
            return pjp.proceed();
        }finally {
            SourceTypeContext.clear();
        }

    }
}
